/*
 * @Autor: dingxiaolin
 * @Date: 2021-01-28 17:20:25
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2021-02-23 14:17:56
 */
import dayjs from 'dayjs';
import { time } from './time';

export const downloadFile = res => {
    if (!res.data) {
        return;
    }
    if (res.headers['content-disposition']) {
        var fileName = res.headers['content-disposition'].split(';')[1].split('filename=')[1];
    } else {
        fileName = time.getFullDate(new Date());
    }
    let url = window.URL.createObjectURL(new Blob([res.data]));
    let link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', decodeURI(fileName));
    document.body.appendChild(link);
    link.click();
};
export const calDays = (startDate, startDateFrom, endDate, endDateFrom) => {
    if (startDate && endDate && startDateFrom != null && endDateFrom != null) {
        var day = dayjs(endDate).diff(dayjs(startDate), 'day');
        if ((startDateFrom == 0 && endDateFrom == 0) || (startDateFrom == 1 && endDateFrom == 1)) {
            day = day + 0.5;
        } else if (startDateFrom == 0 && endDateFrom == 1) {
            day = day + 1;
        }
        return day;
    }
};
